<!--  -->
<template>
  <div>
    <el-table
      :data="userTable"
      border
      style="width: 100%"
      @selection-change="(selection) => tickUser(selection)"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="编号" width="50"></el-table-column>
      <el-table-column prop="uname" label="姓名" width="100"> </el-table-column>
      <el-table-column prop="uemail" label="邮箱" width="300">
      </el-table-column>
      <el-table-column width="100" label="用户状态" prop="ustate">
        <template slot-scope="scope">
          <el-switch
            style="display: block"
            :value="scope.row.ustate"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            active-text="启用"
            @change="changeState(scope.row)"
          >
          </el-switch
        ></template>
      </el-table-column>
      <el-table-column prop="uphone" label="电话" width="200">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small "
            icon="el-icon-edit"
            @click="empEditOne(scope.row)"
            >修改</el-button
          >
          <el-button
            type="warning"
            size="small "
            icon="el-icon-delete"
            @click="empDelOne(scope.row)"
            >删除</el-button
          ></template
        >
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next,sizes,->,total"
      :page-sizes="pageSize"
      :page-size="page.size"
      @current-change="pageChange"
      @size-change="sizeChange"
      :total="page.total"
    >
    </el-pagination>
    <!-- <el-table-column prop="ustate" label="用户状态" width="180">
      </el-table-column> -->

    <el-row :gutter="20">
      <el-col :span="6" :offset="18"
        ><div class="grid-content bg-purple">
          <el-button
            type="primary"
            @click="(dialogVisible = true), (userForm = {}), (flag = 1)"
            icon="el-icon-plus"
            size="small "
            round
            >添加用户</el-button
          >
          <el-button
            type="danger"
            round
            size="small "
            width="130px"
            @click="empDelChoose"
            icon="el-icon-delete-solid
"
            >删除选中</el-button
          >
        </div></el-col
      >
    </el-row>
    <!-- 添加的dialog -->
    <el-dialog title="用户" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="userForm"
        :rules="rules"
        ref="empForm"
        label-width="70px"
        class="demo-empForm"
      >
        <el-form-item label="姓名" prop="uname">
          <el-input v-model="userForm.uname"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="upassword">
          <el-input v-model="userForm.upassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="pwdReg">
          <el-input v-model="userForm.pwdReg" type="password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="uemail">
          <el-input v-model="userForm.uemail"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="uphone">
          <el-input v-model="userForm.uphone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addUser">确定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 编辑的dialog -->
    <el-dialog title="用户" :visible.sync="EnitFlag" width="30%">
      <el-form
        :model="userForm"
        :rules="rules"
        ref="empForm"
        label-width="70px"
        class="demo-empForm"
      >
        <el-form-item label="姓名" prop="uname">
          <el-input v-model="userForm.uname" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="uemail">
          <el-input v-model="userForm.uemail"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="uphone">
          <el-input v-model="userForm.uphone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="flag == 1 ? addPower() : update()"
          >确定</el-button
        >
        <el-button @click="EnitFlag = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    let passwordCheck = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (this.userForm.pwdReg != this.userForm.upassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      flag: 1,
      //默认角色
      rid: 8,
      //添加的列表数据
      userForm: {},
      //弹框的显示和隐藏
      dialogVisible: false,
      EnitFlag: false,
      //用户状态
      userTable: [],
      //勾选的用户信息数组id
      chooseUserId: [],
      //分页对象
      page: { current: 1, size: 7 },
      pageSize: [7, 10, 20],
      //校验的规则
      rules: {
        uname: [{ required: true, message: "请输入姓名!", trigger: "blur" }],
        pwdReg: [{ validator: passwordCheck, trigger: "change" }],
        uemail: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur"],
          },
        ],
        uphone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          // 这个只能验证手机号
          // { pattern:/^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/, message: "请输入合法手机号", trigger: "blur" }
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3-9]\d{9}))$/,
            message: "请输入正确的手机号!",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    pageChange(current) {
      this.page.current = current;
      this.getUserList();
    },
    sizeChange(size) {
      this.page.size = size;
      this.getUserList();
    },

    empDelOne(thisRow) {
      this.$confirm("确认删除该用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.chooseUserId = [];
          this.chooseUserId.push(thisRow.id);
          this.deleteUser();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteUser() {
      this.http({
        url: "user/del",
        method: "delete",
        data: this.chooseUserId,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            message: data.msg,
            type: "success",
          });
          this.getUserList();
        }
      });
    },
    tickUser(selection) {
      this.chooseUserId = [];
      for (const x in selection) {
        this.chooseUserId.push(selection[x].id);
      }
      console.log(this.chooseUserId);
    },
    empDelChoose() {
      if (this.chooseUserId.length == 0) {
        this.$message({
          message: "未勾选,请勾选后再试!",
          type: "warning",
        });
        return;
      }
      this.$confirm("确认删除所有勾选的用户吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteUser();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    empEditOne(thisRow) {
      this.flag = 0;
      this.http({
        url: "user/one/" + thisRow.id,
        method: "get",
      }).then(({ data }) => {
        if (data.code == 1) {
          this.userForm = data.data;
          this.EnitFlag = true;
        }
      });
    },
    update() {
      this.http({
        url: "user/update",
        method: "put",
        data: this.userForm,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            message: data.msg,
            type: "success",
          });
          this.getUserList();
          this.EnitFlag = false;
        }
      });
    },
    addUser() {
      this.http({
        url: "user/save/" + this.rid,
        method: "post",
        data: this.userForm,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            message: data.msg,
            type: "success",
          });
          this.dialogVisible = false;
          this.userForm = {};
          this.getUserList();
        }
      });
    },
    changeState(thisRow) {
      this.http({
        url: "user/updateState/" + thisRow.id,
        method: "put",
      }).then(({ data }) => {
        if (data.code == 1) {
          this.getUserList();
        }
      });
    },
    getUserList() {
      this.http({
        url: "user/page",
        method: "post",
        data: this.page,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.userTable = data.data.records;
          this.page = data.data;
        }
      });
    },
  },

  mounted() {
    this.getUserList();
  },
};
</script>

<style>
</style>
